﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using DbHelper;
using Models;
using Models.SqlitModel;

namespace DAL
{
    public class BooksChapterDAL
    {

        public SqliteEFHelper db;
        public BooksChapterDAL()
        {
            db = new SqliteEFHelper();
        }

        #region 查询章节

        /// <summary>
        /// 
        /// </summary>
        /// <param name="pageSize">分页大小</param>
        /// <param name="pageIndex">页码</param>
        /// <param name="name">章节名</param>
        /// <param name="id">主键</param>
        /// <returns></returns>
        public List<BooksChapter> QueryBooksChapter(ref DataPage page, string name, string url)
        {
            Expression<Func<BooksChapter, bool>> where = PredicateExtensions.True<BooksChapter>();

            if (!string.IsNullOrWhiteSpace(name))
                where = where.And(o => o.Name.Contains(name));
            if (!string.IsNullOrWhiteSpace(url))
                where = where.And(o => o.Href.Contains(url));

            page.DataCount = db.FindAll(where).Count();

            var list = db.FindAll(where)
                .Skip((page.PageIndex - 1) * page.PageSize)
                .Take(page.PageSize);
            return list.ToList();
        }
        /// <summary>
        /// 查询未获取章节的小说
        /// </summary>
        /// <returns></returns>
        public BooksChapter QueryBooksChapter()
        {
            var BooksChapter = db.FindAll<BooksChapter>(o => o.State == "未下载").FirstOrDefault();
            if (BooksChapter != null)
            {
                BooksChapter.State = "下载中";
                db.Update(BooksChapter);
            }
            return BooksChapter;
        }
        /// <summary>
        /// 查询章节
        /// </summary>
        /// <param name="id">ID</param>
        /// <returns></returns>
        public BooksChapter QueryBooksChapterById(string id)
        {
            return db.Find<BooksChapter>(id);
        }
        #endregion

        #region 增删改
        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public BooksChapter InsertBooksChapter(BooksChapter obj)
        {
            return db.Insert(obj);
        }
        /// <summary>
        /// 批量新增
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public int InsertBooksChapter(List<BooksChapter> list)
        {
            return db.InsertEntityFields(list);
        }
        /// <summary>
        /// 更新章节
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public BooksChapter UpdateBook(BooksChapter obj)
        {
            return db.Update(obj);
        }
        /// <summary>
        /// 批量更新章节
        /// </summary>
        /// <param name="list"></param>
        /// <param name="fields"></param>
        /// <returns></returns>
        public int UpdateBooksChapter(List<BooksChapter> list, List<string> fields)
        {
            return db.UpdateEntityFields(list, fields).Count;
        }
        /// <summary>
        /// 物理删除章节
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public int DeleteBooksChapter(Books obj)
        {
            return db.Delete(obj);
        }
        /// <summary>
        /// 批量物理删除章节
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public int DeleteBooksChapter(List<BooksChapter> list)
        {
            return db.Delete(list);
        }
        #endregion
    }
}
